/*
  Revision History:
  Date         Who     Version  What
  2015.1.8     walker  0.0.1    Create this class based on java.util.UUID.
 */
package com.flying.utils.uniquekey;

import java.util.UUID;

public final class IDUtil {
    public static String getUUID() {
        UUID id = UUID.randomUUID();
        long mostSigBits = id.getMostSignificantBits();
        long leastSigBits = id.getLeastSignificantBits();
        return (digits(mostSigBits >> 32, 8) +
                digits(mostSigBits >> 16, 4) +
                digits(mostSigBits, 4) +
                digits(leastSigBits >> 48, 4) +
                digits(leastSigBits, 12));
    }

    /**
     * copy the following function from JDK.
     * Returns val represented by the specified number of hex digits.
     */
    private static String digits(long val, int digits) {
        long hi = 1L << (digits * 4);
        return Long.toHexString(hi | (val & (hi - 1))).substring(1);
    }
}